<?php
class userdao extends CI_Model{
	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}

	public function signup(
			$user_account_name,
			$user_password,
			$user_name,
			$user_surname,
			$default_neighborhood,
			$user_email,
			$user_phone,
			$user_birthdate,
			$ismale,
			$role_id,
			$street_id,
			$remaining_address,
			$branch_phone
	){
		$user_result = $this->userdao->insertuser(
				$user_account_name,
				$user_password,
				$user_name,
				$user_surname,
				$default_neighborhood,
				$user_email,
				$user_phone,
				$user_birthdate,
				$ismale
		);
		if(!$user_result)
		{
			return false;
		}
		else {
			$user_id = $this->db->insert_id();
			$data['user_id'] = $user_id;
			$this->load->model('userhasroledao');
			$role_result = $this->userhasroledao->insert_role_to_user($user_id,$role_id);
			if(!$role_result){
				return false;
			}
			else{
				$this->load->model('useraddressdao');
				$address_result = $this->useraddressdao->insert($user_id,
						$street_id,
						$remaining_address,
						$branch_phone
				);
				if ($address_result) {
					return $data;
				}
				else
					return false;
			}
			return false;
		}
		return false;
	}

	public function getbyid($id){
		$this->db->select('*');
		$this->db->from('user');
		$this->db->where('user_id',$id);
		$query = $this->db->get();
		$user = $query->row();
		return $user;
	}

	public function getusers(){
		$this->db->select('*');
		$this->db->from('user');
		$query = $this->db->get();
		$user = $query->result();
		return $user;
	}

	public function getbyemail($email){
		$this->db->select('*');
		$this->db->from('user');
		$this->db->where('user_email',$email);
		$query = $this->db->get();
		$id = $query->row()->user_id;
		return $id;
	}

	public function insertuser(
			$user_account_name,
			$user_password,
			$user_name,
			$user_surname,
			$default_neighborhood_id,
			$user_email,
			$user_phone,
			$user_birthdate,
			$ismale
	){
		$data = array(
				'user_account_name' => $user_account_name,
				'user_password' => $user_password,
				'user_name' => $user_name,
				'user_surname' => $user_surname,
				'default_neighborhood_id' => $default_neighborhood_id,
				'user_email' => $user_email,
				'user_phone' => $user_phone,
				'user_birthdate' => $user_birthdate,
				'user_is_male' => $ismale
		);
		$str = $this->db->insert_string('user', $data);
		$res = $this->db->query($str);
		if ($res) {
			return true;
		} else {
			return false;
		}
	}

	public function login($uname,$pass){
		$this->db->select('*');
		$this->db->from('user');
		$this->db->where('user_account_name',$uname);
		$this->db->where('user_password',$pass);
		$query = $this->db->get();
		if($query->num_rows() == 1){
			return $query->row();
		} else {
			return 0;
		}
	}

	public function del($user_id) {
		if (intval($user_id) > 0) {
			$this->db->where('user_id', $user_id);
			$this->db->delete('user');
			return ($this->db->affected_rows() == 1) ? TRUE : FALSE;
		}
		return false;
	}

}
?>